
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.sys.mapper.BscDicCodeItemMapper;
import com.example.demo.modules.sys.model.BscDicCodeItem;
import com.example.demo.modules.sys.model.BscDicCodeType;
import com.example.demo.modules.sys.model.SysMenu;
import com.example.demo.modules.sys.service.DictService;
import com.example.demo.modules.sys.service.MenuService;
import com.example.demo.modules.sys.vo.SysMenuVo;
import com.example.demo.modules.sys.vo.SysUserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.example.demo.core.entity.ProcessResult.ERROR;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/data/dic")
public class DicController {
    @Autowired
    private DictService dictService;

    @RequestMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("/modules/data/dic/list");
    }

    @RequestMapping("/listItem/{id}")
    public PageResult<BscDicCodeItem> listItem(@PathVariable Integer id,BscDicCodeItem bscDicCodeItem,String keyword){
        bscDicCodeItem.setTypeId(id);
        List<BscDicCodeItem> list = dictService.listItem(bscDicCodeItem, keyword);
        return new PageResult<>(new PageInfo<>(list));
    }

    @RequestMapping("/listType")
    public PageResult<BscDicCodeType> listType(BscDicCodeType bscDicCodeType, String keyword){
        List<BscDicCodeType> list=dictService.listType(bscDicCodeType,keyword);
        return new PageResult(new PageInfo(list));
    }

    @RequestMapping("/saveOrUpdateItem")
    public ProcessResult saveOrUpdateItem(BscDicCodeItem bscDicCodeItem){
        try {
            ProcessResult result= new ProcessResult();
            dictService.saveOrUpdateItem(bscDicCodeItem);
            result.setData(bscDicCodeItem.getTypeId());
            return result;
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }
    @RequestMapping("/deleteItem/{id}")
    public ProcessResult deleteItem(@PathVariable Integer id){
        try {
            dictService.deleteItem(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }
    @RequestMapping("/view/{id}")
    public BscDicCodeItem view(@PathVariable Integer id){
        return dictService.view(id);
    }

    @RequestMapping("/saveOrUpdateType")
    public ProcessResult saveOrUpdateType(BscDicCodeType bscDicCodeType){
        try {
            ProcessResult result=new ProcessResult();
            dictService.saveOrUpdateType(bscDicCodeType);
            result.setData(bscDicCodeType.getId());
            return result;
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }

    }

    @RequestMapping("/deleteType/{id}")
    public ProcessResult deleteType(@PathVariable Integer id){
        try {
            dictService.deleteType(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping("/viewType/{id}")
    public BscDicCodeType viewType(@PathVariable Integer id){
        return dictService.viewType(id);
    }

    /**
     *
     * @param typeCode
     * @return
     */
    @RequestMapping(value = "/getBscDicCodeItemListByTypeCode/{typeCode}")
    public List<BscDicCodeItem> getBscDicCodeItemListByTypeCode(@PathVariable String typeCode){
        return dictService.getBscDicCodeItemListByTypeCode(typeCode);
    }

    /**
     *
     * @param typeCode
     * @param itemCode
     * @return
     */
    @RequestMapping(value = "/getBscDicCodeItemListByTypeCode/{typeCode}/{itemCode}")
    public BscDicCodeItem getBscDicCodeItemListByTypeCodeAndItemCode(@PathVariable String typeCode,@PathVariable String itemCode){
        return dictService.getBscDicCodeItemListByTypeCodeAndItemCode(typeCode,itemCode);
    }


}
